<?php
class AnswersController extends AppController {

	var $name = 'Answers';
	var $uses = array('Answer','Question');
  var $scaffold = 'admin';

	function index() {
		$this->Answer->recursive = 0;
		$this->set('Answers', $this->paginate());
	}

  
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Answer', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('Answer', $this->Answer->read(null, $id));
	}
	
	function accept($id = null)
	{

		if (!$id) {
			$this->cakeError('not_found');
		}
		$this->Answer->recursive = -1;
		$answer = $this->Answer->findById($id);
		$question = $this->Question->findById($answer['Answer']['question_id']);
		$this->Question->id = $answer['Answer']['question_id'];
		if (!$answer or $this->cuid!=$question['Question']['user_id'] or 
			$question['Question']['status']==Question::SOLVED or 
			$question['Question']['status']==Question::CLOSED) 	
		{
			$this->cakeError('Access');
		}
		$this->Answer->id = $id;
		$this->Answer->saveField('marked_as',Answer::BEST);
		$this->User->id = $answer['Answer']['user_id'];
		$answerer = $this->User->findById($answer['Answer']['user_id']);
		$points = $answerer['User']['points'];
		$this->User->saveField('points',$points+Answer::BEST_SCORE);
		$this->Question->saveField('status',Question::SOLVED);
		$this->redirect($this->referer());	
		
	}
	
	
	function helpful($id = null)
	{
		if (!$id) {
			$this->cakeError('not_found');
		}
		$this->Answer->recursive = -1;
		$answer = $this->Answer->findById($id);
		$question = $this->Question->findById($answer['Answer']['question_id']);
		$this->Question->id = $answer['Answer']['question_id'];
		if (!$answer or $this->cuid!=$question['Question']['user_id'] or 
			$question['Question']['status']==Question::SOLVED or 
			$question['Question']['status']==Question::CLOSED) 	
		{
			$this->cakeError('Access');
		}
		$this->Answer->id = $id;
		$this->Answer->saveField('marked_as',Answer::HELPFUL);
		$this->User->id = $answer['Answer']['user_id'];
		$answerer = $this->User->findById($answer['Answer']['user_id']);
		$points = $answerer['User']['points'];
		$this->User->saveField('points',$points+Answer::HELPFUL_SCORE);
		$this->Question->saveField('status',Question::SOLVED);
		$this->redirect($this->referer());
	}
	function add() {
		if (!empty($this->data)) {
			$this->Answer->create();
			if ($this->Answer->save($this->data)) {
				$this->Session->setFlash(__('The Answer has been saved', true));
				$this->redirect($this->referer());
			} else {
				$this->Session->setFlash(__('The Answer could not be saved. Please, try again.', true));
			}
		}
		$users = $this->Answer->User->find('list');
		$questions = $this->Answer->Question->find('list');
		$this->set(compact('users', 'questions'));
	}
	
	function ajax_add()
	{
		if (!emtyp($this->data) and $this->RequestHandler->isAjax()) {
			$this->Answer->create();
			if ($this->Answer->save($this->data)) {	
				
			}else{
				
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Answer', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Answer->save($this->data)) {
				$this->Session->setFlash(__('The Answer has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Answer could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Answer->read(null, $id);
		}
		$users = $this->Answer->User->find('list');
		$questions = $this->Answer->Question->find('list');
		$this->set(compact('users', 'questions'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Answer', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Answer->delete($id)) {
			$this->Session->setFlash(__('Answer deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Answer was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>